package travianBot;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.HttpClient;
import org.apache.log4j.Logger;


public class TravianBotUtil {
	public static final String NEWLINE = System.getProperty("line.separator");
	static Logger log=Logger.getLogger(TravianBotUtil.class);
	
	
	public static String getFileAsString(String filename) throws IOException{
		FileReader reader = new FileReader(filename);
		BufferedReader bReader = new BufferedReader(reader);
		
		StringBuilder builder = new StringBuilder();
		String line="";
		while(null != ( line = bReader.readLine())){
			builder.append(line+NEWLINE);
		}
		
		return builder.toString();
	}
	
	public static String stringPad(String initialString, String padding, int padNumber){
		for (int i = 0; i < padNumber; i++) {
			initialString+=padding;
		}
		return initialString;
	}
	
	public static String getHttpPostContents(HttpClient httpclient, HashMap<String,String> parameters, String url) throws TravianBotException{
		StringBuilder responseString = new StringBuilder();
		
		PostMethod httpPost = new PostMethod(url);
		// setup the parameters for login
		for (String key : parameters.keySet()) {
			httpPost.addParameter(key,parameters.get(key));
		}
				
		try {
			log.debug("HTTPost to URL:"+httpPost.getURI());
			httpclient.executeMethod(httpPost);
			/*
	         * Logic to capture links and optionally store the captured html in a file
	         */
			BufferedReader in = new BufferedReader(new InputStreamReader(httpPost.getResponseBodyAsStream()));
			String line;
	        while(null != (line = in.readLine())){
	        	responseString.append(line+NEWLINE);
	        }
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new TravianBotException(e);			
		}     
		
		// fake think time
		TravianBot.thinkTimeSleep();
		
		return responseString.toString();
	}
	
	public static void sendEmail(String smtpServer, String to, String from,
			String subject, String body) {
		try {
			Properties props = System.getProperties();
			// -- Attaching to default Session, or we could start a new one --
			props.put("mail.smtp.host", smtpServer);
			Session session = Session.getDefaultInstance(props, null);
			// -- Create a new message --
			Message msg = new MimeMessage(session);
			// -- Set the FROM and TO fields --
			msg.setFrom(new InternetAddress(from));
			msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
					to, false));
			// -- We could include CC recipients too --
			// if (cc != null)
			// msg.setRecipients(Message.RecipientType.CC
			// ,InternetAddress.parse(cc, false));
			// -- Set the subject and body text --
			msg.setSubject(subject);
			msg.setText(body);
			// -- Set some other header information --
			msg.setHeader("X-Mailer", "LOTONtechEmail");
			msg.setSentDate(new Date());
			// -- Send the message --
			Transport.send(msg);
			//System.out.println("Message sent OK.");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
